package com.rickpan.entity;

import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 团队消息实体类
 * 支持文本、图片、文件三种消息类型
 * 
 * @author 开发团队协调员
 * @since 2025-07-16
 */
@Entity
@Table(name = "team_messages")
@EntityListeners(AuditingEntityListener.class)
public class TeamMessage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "team_id", nullable = false)
    private Long teamId;

    @Column(name = "sender_id", nullable = false)
    private Long senderId;

    @Enumerated(EnumType.STRING)
    @Column(name = "message_type", nullable = false)
    private MessageType messageType = MessageType.text;

    @Column(columnDefinition = "TEXT", nullable = false)
    private String content;

    @Column(name = "file_name")
    private String fileName;

    @Column(name = "file_size")
    private Long fileSize;

    @Column(name = "file_url", length = 500)
    private String fileUrl;

    @Enumerated(EnumType.STRING)
    @Column(name = "message_status", nullable = false)
    private MessageStatus messageStatus = MessageStatus.sent;

    @Column(name = "reply_to_id")
    private Long replyToId;

    // 加密相关字段
    @Column(name = "iv", length = 32)
    private String iv;

    @Column(name = "encryption_algorithm", length = 50)
    private String encryptionAlgorithm = "AES-256-GCM";

    @Column(name = "key_version")
    private Integer keyVersion = 1;

    @Column(name = "is_encrypted", nullable = false)
    private Boolean isEncrypted = true;

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    // 关联实体（懒加载）
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "team_id", insertable = false, updatable = false)
    private Team team;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sender_id", insertable = false, updatable = false)
    private User sender;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reply_to_id", insertable = false, updatable = false)
    private TeamMessage replyToMessage;

    // 枚举定义
    public enum MessageType {
        text,    // 文本消息
        image,   // 图片消息
        file     // 文件消息
    }

    public enum MessageStatus {
        sending, // 发送中
        sent,    // 已发送
        failed   // 发送失败
    }

    // 构造函数
    public TeamMessage() {}

    public TeamMessage(Long teamId, Long senderId, MessageType messageType, String content) {
        this.teamId = teamId;
        this.senderId = senderId;
        this.messageType = messageType;
        this.content = content;
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTeamId() {
        return teamId;
    }

    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    public Long getSenderId() {
        return senderId;
    }

    public void setSenderId(Long senderId) {
        this.senderId = senderId;
    }

    public MessageType getMessageType() {
        return messageType;
    }

    public void setMessageType(MessageType messageType) {
        this.messageType = messageType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public MessageStatus getMessageStatus() {
        return messageStatus;
    }

    public void setMessageStatus(MessageStatus messageStatus) {
        this.messageStatus = messageStatus;
    }

    public Long getReplyToId() {
        return replyToId;
    }

    public void setReplyToId(Long replyToId) {
        this.replyToId = replyToId;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public User getSender() {
        return sender;
    }

    public void setSender(User sender) {
        this.sender = sender;
    }

    public TeamMessage getReplyToMessage() {
        return replyToMessage;
    }

    public void setReplyToMessage(TeamMessage replyToMessage) {
        this.replyToMessage = replyToMessage;
    }

    // 加密字段的getter和setter
    public String getIv() {
        return iv;
    }

    public void setIv(String iv) {
        this.iv = iv;
    }

    public String getEncryptionAlgorithm() {
        return encryptionAlgorithm;
    }

    public void setEncryptionAlgorithm(String encryptionAlgorithm) {
        this.encryptionAlgorithm = encryptionAlgorithm;
    }

    public Integer getKeyVersion() {
        return keyVersion;
    }

    public void setKeyVersion(Integer keyVersion) {
        this.keyVersion = keyVersion;
    }

    public Boolean getIsEncrypted() {
        return isEncrypted;
    }

    public void setIsEncrypted(Boolean isEncrypted) {
        this.isEncrypted = isEncrypted;
    }

    // 便利方法
    public boolean isTextMessage() {
        return MessageType.text.equals(this.messageType);
    }

    public boolean isImageMessage() {
        return MessageType.image.equals(this.messageType);
    }

    public boolean isFileMessage() {
        return MessageType.file.equals(this.messageType);
    }

    public boolean isSent() {
        return MessageStatus.sent.equals(this.messageStatus);
    }

    public boolean isFailed() {
        return MessageStatus.failed.equals(this.messageStatus);
    }

    public boolean isEncrypted() {
        return Boolean.TRUE.equals(this.isEncrypted);
    }

    public boolean hasEncryptionData() {
        return isEncrypted() && iv != null && !iv.isEmpty();
    }

    @Override
    public String toString() {
        return "TeamMessage{" +
                "id=" + id +
                ", teamId=" + teamId +
                ", senderId=" + senderId +
                ", messageType=" + messageType +
                ", content='" + content + '\'' +
                ", messageStatus=" + messageStatus +
                ", createdAt=" + createdAt +
                '}';
    }
}
